//
//  MyPublicVC.m
//  MZJ
//
//  Created by XDD on 16/6/4.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "MyPublicVC.h"
#import "MyPublicCell.h"
#import "MyPublic_EditVC.h"
#import "MJRefresh.h"
#import "MyPublic_DetailVC.h"

@interface MyPublicVC () <UITableViewDelegate,UITableViewDataSource> {
    NSInteger page;
}

@property (nonatomic, strong) UITableView *myPublic_TabelView;
@property (nonatomic, strong) NetWorkingSModel *model;
@property (nonatomic, strong) ModelViewController *modelViewController;
@property (nonatomic, strong) NSMutableArray *allarr;
@property (nonatomic, strong) NSArray *arr2;

@end

@implementation MyPublicVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    [self requestnetwork];

//        [weakSelf.model userGetMy_issue:dic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topBar.backgroundColor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:10/255.0 alpha:1];
    self.titleLabel.text = @"我的求助";
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    [self.backButton setTitle:@"" forState:UIControlStateNormal];
    
    UIButton *seek_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    seek_Btn.frame = CGRectMake(kWidth - 40, 25, 35, 25);
    [seek_Btn setTitle:@"发布" forState:UIControlStateNormal];
    [seek_Btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    seek_Btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [seek_Btn addTarget:self action:@selector(seek_BtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.topBar addSubview:seek_Btn];
    
    self.myPublic_TabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 55, kWidth, kHeight - 55)];
    self.myPublic_TabelView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    self.myPublic_TabelView.delegate = self;
    self.myPublic_TabelView.dataSource = self;
    [self.myPublic_TabelView registerNib:[UINib nibWithNibName:@"MyPublicCell" bundle:nil] forCellReuseIdentifier:@"myPublicCell"];
    self.myPublic_TabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myPublic_TabelView];
    
    
    page = 1;
    //    _allarr = [NSMutableArray array];
    _arr2 = [NSArray array];
    //调网络请求接口
    if (!_modelViewController) {
        _modelViewController = [[ModelViewController alloc]init];
        if (!_model) {
            _model = [[NetWorkingSModel alloc] init];
        }
    }
    
    //下拉刷新
    self.myPublic_TabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _allarr = nil;
        if (_allarr == nil) {
            page = 1;
            [self requestnetwork];
        }
        // 结束刷新
        [self.myPublic_TabelView.mj_header endRefreshing];
        
    }];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.myPublic_TabelView.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉刷新
    self.myPublic_TabelView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [self requestnetwork];
        // 结束刷新
        [self.myPublic_TabelView.mj_footer endRefreshing];
    }];
}

-(void)requestnetwork{
    
    __weak typeof (self)weakSelf = self;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *token = [user objectForKey:@"user_token"];
    
    if (token.length != 0) {
        [dic setObject:token forKey:@"token"];
        [dic setObject:[NSString stringWithFormat:@"%ld",(long)page] forKey:@"page"];
    }else {
        [dic setObject:[NSString stringWithFormat:@"%ld",(long)page] forKey:@"page"];
    }
    [_model setBlockWithReturnBlock:^(id returnValue) {
        
        NSLog(@"我的求助 = %@",returnValue);
        weakSelf.arr2 = returnValue[@"result"];
        
        if ( page == 1) {
            _allarr = [NSMutableArray array];
        }
        [weakSelf.allarr addObjectsFromArray:weakSelf.arr2];
        [weakSelf.myPublic_TabelView reloadData];
        [weakSelf.myPublic_TabelView.mj_footer resetNoMoreData];
        
    } WithErrorBlock:^(id errorCode) {
        NSLog(@"没有更多数据");
        [weakSelf.myPublic_TabelView.mj_footer endRefreshingWithNoMoreData];
    }];
    [weakSelf.model userGetMy_issue:dic];
}

//发布求助按钮
- (void)seek_BtnAction {
    MyPublic_EditVC *myPublic_EditVC = [[MyPublic_EditVC alloc]init];
    [self.navigationController pushViewController:myPublic_EditVC animated:YES];
}

//点击返回键
- (void)clickedBackButton:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (_allarr.count == 0) {
        return 0;
    }
    return _allarr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 80;
}

//Header的高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    
    return 5*XJVerticalMultiplier();
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *head = [[UIView alloc]init];
    head.frame = XJFlexibleFrame(CGRectMake(0, 0, 320, 5), NO);
    head.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    
    return head;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSInteger section = indexPath.section;
    
    static NSString *identifier = @"myPublicCell";
    MyPublicCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (!cell) {
        
        cell = [[MyPublicCell alloc] initWithFrame:CGRectZero];
    }
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:_allarr[indexPath.section][@"img"]]
                          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    cell.phoneNumLabel.text = [NSString stringWithFormat:@"联系方式 %@",_allarr[indexPath.section][@"mobile"]];
    cell.nameLabel.text = _allarr[indexPath.section][@"pname"];
    cell.dateLabel.text = [_modelViewController TimeStamp:_allarr[indexPath.section][@"pstarttime"] form_time:@"yyyy-MM-dd"];
    cell.masegessLabel.text = _allarr[indexPath.section][@"pdescription"];
    if ([_allarr[indexPath.section][@"pcheck"] intValue] == 0) {
        cell.examine_Label.textColor = [UIColor colorWithRed:76/255.0 green:152/255.0 blue:218/255.0 alpha:1];
        cell.examine_imageView.image = [UIImage imageNamed:@"examine_ progress_img@2x"];
        cell.examine_Label.text = @"审核中";
    }
    if ([_allarr[indexPath.section][@"pcheck"] intValue] == 1) {
        cell.examine_Label.text = @"已通过";
        cell.examine_Label.textColor = [UIColor colorWithRed:26/255.0 green:139/255.0 blue:24/255.0 alpha:1];
        cell.examine_imageView.image = [UIImage imageNamed:@"examine_scusess_img@2x"];

    }
    if ([_allarr[indexPath.section][@"pcheck"] intValue] == 2) {
        cell.examine_Label.text = @"未通过";
        cell.examine_Label.textColor = [UIColor colorWithRed:205/255.0 green:29/255.0 blue:34/255.0 alpha:1];
        cell.examine_imageView.image = [UIImage imageNamed:@"examine_lose_img@2x"];

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [DataModel object].myPublic_Detail_Str = _allarr[indexPath.section][@"pid"];

    MyPublic_DetailVC *myPublic_DetailVC = [[MyPublic_DetailVC alloc]init];
    [self.navigationController pushViewController:myPublic_DetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
